//
//  YUTabbarView.m
//  QQZone
//
//  Created by 张洪毓 on 15/11/25.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

#import "YUTabbarView.h"
#import "YUTabbarButton.h"
#import "UIImage+XL.h"

@interface YUTabbarView ()

@property (nonatomic,strong) YUTabbarButton *selectedButton;

@end

@implementation YUTabbarView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setButtonWithIcon:@"tab_bar_feed_icon" title:@"全部动态"];
        [self setButtonWithIcon:@"tab_bar_passive_feed_icon" title:@"与我相关"];
        [self setButtonWithIcon:@"tab_bar_pic_wall_icon" title:@"照片墙"];
        [self setButtonWithIcon:@"tab_bar_e_album_icon" title:@"电子相框"];
        [self setButtonWithIcon:@"tab_bar_friend_icon" title:@"好友"];
        [self setButtonWithIcon:@"tab_bar_e_more_icon" title:@"更多"];
    }
    return self;
}

- (void)setButtonWithIcon:(NSString *)icon title:(NSString *)title
{
    YUTabbarButton *button = [[YUTabbarButton alloc] init];
    [button setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage resizableImage:@"tabbar_separate_selected_bg"] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
    [self addSubview:button];
}

-(void)buttonClick:(YUTabbarButton *)btn{
    
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
}

-(void)setIsLandscape:(BOOL)isLandscape{
    _isLandscape = isLandscape;
    
    NSUInteger count = self.subviews.count;
    for(int i = 0; i < count; i++) {
        YUTabbarButton *btn = self.subviews[i];
        CGFloat X = 0;
        CGFloat Y = i * kButtonWidth;
        CGFloat W = self.superview.width;
        CGFloat H = kButtonWidth;
        btn.frame = CGRectMake(X, Y, W, H);
    }
    
    self.height = count * kButtonWidth;
}

@end
